/**
 Copyright (C) 2013 Yuyao Liang

 This source code is release under the BSD License.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.

 Created on Oct 23, 2013 2:06:36 PM
 */
package com.google.srclib.math;

import java.sql.SQLException;

import com.google.srclib.dataset.StockRecordDataset;

/**
 * @author lianyuy
 *
 */
public class StockAdjustmentUtility {

	/**
	 * @param sd
	 * @return
	 * @throws SQLException
	 */
	public static StockRecordDataset adjustRecordDataset(StockRecordDataset sd)
			throws SQLException {
		if (sd.next()) {
			sd.beforeFirst();
			while (sd.next()) {
				double high = sd.getDouble(StockRecordDataset.HIGH);
				double low = sd.getDouble(StockRecordDataset.LOW);
				double open = sd.getDouble(StockRecordDataset.OPEN);
				double close = sd.getDouble(StockRecordDataset.CLOSE);
				double volumn = sd.getDouble(StockRecordDataset.VOLUMN);
				double adjclose = sd.getDouble(StockRecordDataset.ADJCLOSE);
				double ratio = adjclose / close;
				sd.updateDouble(StockRecordDataset.HIGH, high * ratio);
				sd.updateDouble(StockRecordDataset.LOW, low * ratio);
				sd.updateDouble(StockRecordDataset.OPEN, open * ratio);
				sd.updateDouble(StockRecordDataset.CLOSE, close * ratio);
				sd.updateDouble(StockRecordDataset.VOLUMN, volumn * ratio);
			}
			sd.beforeFirst();
		}
		return sd;
	}


}
